package com.txw.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.txw.sercicebase.exceptionhandller.LingqiException;
import com.txw.serviceedu.entity.EduChapter;
import com.txw.serviceedu.entity.EduVideo;
import com.txw.serviceedu.entity.chapter.ChapterVo;
import com.txw.serviceedu.entity.chapter.VideoVo;
import com.txw.serviceedu.mapper.EduChapterMapper;
import com.txw.serviceedu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.txw.serviceedu.service.EduVideoService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author TanXiongWei
 * @since 2021-11-28
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    //课程大纲列表根据id查询
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //根据id查询所有章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id", courseId);
        List<EduChapter> eduChaptersList = baseMapper.selectList(wrapperChapter);

        //根据id查询课程里所有小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        List<EduVideo> eduVideoList = eduVideoService.list(wrapperVideo);

        List<ChapterVo> finalList = new ArrayList<>();//创建list集合封装数据

        //遍历查询章节list集合进行封装
        for (int i = 0; i < eduChaptersList.size(); i++) {
            EduChapter eduChapter = eduChaptersList.get(i);//每个章节
            //eduChapter里面的值复制到chapterVo里面
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            finalList.add(chapterVo);

            List<VideoVo> videoVoList = new ArrayList<>();//创建list集合封装章节中的小节
            for (int j = 0; j < eduVideoList.size(); j++) { //遍历小节中的集合，进行封装
                EduVideo eduVideo = eduVideoList.get(j); //得到每个小节
                if (eduVideo.getChapterId().equals(eduChapter.getId())) {//判断小节里chapterid跟章节里是否一样
                    //进行封装
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videoVoList.add(videoVo);
                }

            }
            chapterVo.setChaldren(videoVoList);//把封装之后的小节放到章节对象里面
        }
        return finalList;
    }

    //删除章节方法
    @Override
    public boolean deleteChapter(String chapterId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id", chapterId);
        int count = eduVideoService.count(wrapper);
        if (count > 0) {//有数据不进行删除
            throw new LingqiException(20001, "不能删除");
        } else {  //没数据删除
            int result = baseMapper.deleteById(chapterId);
            return result > 0;//大于0范回true/小于0范回false
        }

    }


    /**
     * 根据课程id删除章节
     * @param courseId
     */
    @Override
    public void removeChaoterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
